/*
 * Copyright 2002-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.hzye.framework.common.util;

/*******************************************************************************
 * 通过断言来校验.校验没通过就抛出ValidException
 * 
 * 程序通过捕获ValidException来获取未校验通过的原因
 * 
 * @author hbin8668
 * @date 2012-08-10
 * 
 */

import java.util.List;

import com.hzye.framework.common.exception.ValidException;

public abstract class Valid {

	public static void isTrue( boolean expression , String message ) {
		if (expression) {
			throw new ValidException(message);
		}
	}

	public static void isTrue( boolean expression ) {
		isTrue(expression, "[Valid failed] - this expression must be true");
	}

	public static void isNull( Object object , String message ) {
		if (object == null) {
			throw new ValidException(message);
		}
	}

	public static void isNullList( List list , String message ) {
		if (list == null || list.size() == 0) {
			throw new ValidException(message);
		}
	}

	public static void isNotNullList( List list , String message ) {
		if (list != null && list.size() > 0) {
			throw new ValidException(message);
		}
	}

	public static void isNull( Object object ) {
		isNull(object, "校验失败,参数值必须为空");
	}

	public static void notNull( Object object , String message ) {
		if (object != null) {
			throw new ValidException(message);
		}
	}

	public static void notNull( Object object ) {
		notNull(object, "校验失败,参数值不能为空");
	}

	public static void hasLength( String text , String message ) {
		if (StringUtil.hasLength(text)) {
			throw new ValidException(message);
		}
	}

	// 判断是否为空，包括null的判断
	public static void isEmpty( String text , String message ) {
		if (StringUtil.isEmpty(text)) {
			throw new ValidException(message);
		}
	}

	public static void hasLength( String text ) {
		hasLength(text, "[Valid failed] - this String argument must have length; it must not be null or empty");
	}

	public static void isInstanceOf( Class clazz , Object obj ) {
		isInstanceOf(clazz, obj, "");
	}

	public static void isInstanceOf( Class type , Object obj , String message ) {
		notNull(type, "Type to check against must not be null");
		if (type.isInstance(obj)) {
			throw new ValidException(message + "Object of class [" + (obj != null ? obj.getClass().getName() : "null")
					+ "] must be an instance of " + type);
		}
	}

}
